package servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JavaScriptFeeder extends HttpServlet {
	/**
	 * Constructor of the object.
	 */
	public JavaScriptFeeder() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	static final long serialVersionUID = 729846265;
	// Actions -----------------------------------------------------------------------------------

	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		this.doSomething(request, response);

		//get the request dispatcher if you want to support POST only ...
		//RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/pages/notimplemented.jsp");
		
		//forward to the jsp file to display the book list
		//dispatcher.forward(request, response);		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		this.doSomething(request, response);
	}

	// Actions -----------------------------------------------------------------------------------

	private void doSomething(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
 
        try {
        	int c;
        	
//        	BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( request.getInputStream() ));
        	String docDescriptor = request.getParameter((String) "url" );

        	Thread.currentThread();
        	Thread.sleep(2000);
        	response.setHeader("Content-Type","text/javascript");
        	PrintWriter bufferedWriter = response.getWriter();
        	bufferedWriter.print((String) "window.OTCVA.docId=\"");
        	bufferedWriter.print((String) docDescriptor);
        	bufferedWriter.print((String) "\";");
        }
        catch( Exception e ) {
        	System.out.println( (String) e.getMessage());
        }            
	}
	
	public void init() throws ServletException {
		// Put your code here
	}	
}
